package cn.snowsoft.iot.module.cps.mqtt;

import cn.snowsoft.iot.module.cps.constant.CpsConstant;
import cn.snowsoft.iot.module.cps.dal.dataobject.equipment.EquipmentDO;
import cn.snowsoft.iot.module.cps.service.equipment.EquipmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
@Slf4j
public class InitCpsServer {

	private final EquipmentService equipmentService;
	public static ConcurrentHashMap<String, String> deviceCodeAttributeTopic = new ConcurrentHashMap<>();

	public static ConcurrentHashMap<String, String> deviceCodeFeatureTopic = new ConcurrentHashMap<>();

	public static ConcurrentHashMap<String, String> protocolAgreement = new ConcurrentHashMap<>();

	@PostConstruct
	private void process() {
		equipmentService.lambdaQuery()
				.eq(EquipmentDO::getIsEnable, CpsConstant.ENABLE_STATUS)
				.list()
				.stream().forEach(e -> {
					deviceCodeAttributeTopic.put(e.getEquipmentCode(), e.getAttributePush());
					deviceCodeFeatureTopic.put(e.getEquipmentCode(), e.getFeatureIssued());
				});
	}
}
